define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    $("select[name='row[params_type_id]']").change(function(){
        var form = $("form[role='form']").attr('id');
        var typeVal = $(this).val();
        if(form=='add-form'){
            if(typeVal == 4 ){
                $('#detailDiv').hide();
                $('.keyWordDiv').show();
            }else{
                $('#detailDiv').show();
                $('.keyWordDiv').hide();
            }
        }else if(form=='edit-form'){
            if(typeVal == 4 ){
                $('#c-Detail').attr("readonly",true);
                $('.keyWordDiv').show();
            }else{
                $('#c-Detail').removeAttr("readonly");
                $('.keyWordDiv').hide();
            }
        }

        Controller.api.bindevent();
    });
    var Controller = {
        index: function () {
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "任务名称-搜索";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'task_config/index' + location.search,
                    add_url: 'task_config/add',
                    edit_url: 'task_config/edit',
                    del_url: 'task_config/del',
                    multi_url: 'task_config/multi',
                    import_url: 'task_config/import',
                    table: 'task_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'Id',
                sortName: 'Id',
                pageSize:30,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'Id', title: __('Id')},
                        {field: 'analyze',
                            title: '结果分析',
                            formatter:function (value,row) {
                                var url_str = '/tTrHFCUqIV.php/task_result_analyze?config_id='+row.Id;
                                return '<a class="btn-addtabs" href="'+url_str+'">'+value+'</a >';
                            }
                        },
                        {field: 'Name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'paramstype.name', title: __('请求类型type'), operate: 'LIKE'},
                        {field: 'paramstype.name', title: __('请求类型type'), visible: true, addClass: "selectpage", extend: "data-source='params_type' data-field='name' data-primary-key='name'"},
                        {field: 'Url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {
                            field: 'TargetUrl',
                            title: __('目标地址'),
                            operate: 'LIKE',
                            formatter: Table.api.formatter.url,
                        },
                        {field: 'Eigenvalue', title: __('Eigenvalue'), operate: 'LIKE'},
                        {field: 'Count', title: __('Count')},
                        {field: 'count_actual', title: __('返回数量'),operate: false},
                        {field: 'actual_quantity', title: __('实际执行数量'),operate: false},
                        {field: 'click_nums', title: __('广告点击数量'),operate: false},
                        {field: 'TimeStart', title: __('Timestart')},
                        {field: 'TimeEnd', title: __('Timeend')},
                        {field: 'DateStart', title: __('Datestart'), operate: 'LIKE'},
                        {field: 'DateEnd', title: __('Dateend'), operate: 'LIKE'},
                        // {field: 'TimeSpan', title: __('TimeSpan'), operate: true},
                        {field: 'TimeSpan', title: __('时间间隔')},
                        {field: 'Detail', title: __('Detail'), operate: 'LIKE',align: 'left',cellStyle: Controller.api.formatter.css},
                        {field: 'Status', title: __('Status')},
                        {field: 'params_type_id', title: __('params_type_id'), visible: false},
                        //formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
                        {field: 'create_time', title: __('Create_time'),formatter: Table.api.formatter.datetime, operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'update_time', title: __('Update_time'),formatter: Table.api.formatter.datetime, operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table',function(e,data){
                $.ajax({
                    type: "GET",
                    url: 'task_config/actual_quantity',
                    dataType: "json", //声明成功使用json数据类型回调
                    success: function(res) {
                        console.log(res);
                        for (var i = 0; i < res.length; i++) {
                            let round_config_id = res[i].round_config_id;
                            let total = res[i].total;
                            $('#table tbody tr').each(function(){
                                let id = $(this).find('td').eq(1).html();
                                if (round_config_id == id) {
                                    $(this).find('td').eq(9).html(total);
                                };
                            })
                        };
                    }
                })
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            var type = $("select[name='row[params_type_id]']").val();
            if(type == 4 ){
                // $('#detailDiv').hide();
                $('#c-Detail').attr("readonly",true)
                $('.keyWordDiv').show();
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                css:function(){
                    return{
                        css:{
                            "max-width":"450px !important",
                            "overflow":"hidden",
                            "white-space":"nowrap",
                            "text-overflow":"ellipsis",
                            "display":"block",
                        }
                    };
                },
                url: function (value) {
                    value = value == null || value.length === 0 ? '' : value.toString();
                    return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm url-target" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                }
            }
        }
    };
    return Controller;
});
